\ProvidesPackage{kaorefs}

%----------------------------------------------------------------------------------------
%	PACKAGE OPTIONS AND DEPENDENCIES
%----------------------------------------------------------------------------------------

% Easily label and reference elements with this package
% Note that, in figures and tables, \label must appear after \caption
% Load this package last

% Pass this package's options to hyperref and varioref
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{varioref}}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{hyperref}}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{cleveref}}
\ProcessOptions\relax

\let\thmname\relax % Workaround to get rid of an annoying error
\RequirePackage{amsthm} % Needed by cleveref
\RequirePackage{varioref}
\RequirePackage{hyperref}
\RequirePackage[capitalise,nameinlink,noabbrev]{cleveref}


%----------------------------------------------------------------------------------------
%	LANGUAGE-SPECIFIC STRINGS
%----------------------------------------------------------------------------------------

% #LANGUAGE
\newcommand{\chapternameshort}{}
\newcommand{\sectionname}{}
\newcommand{\sectionnameshort}{}
\newcommand{\subsectionname}{}
\newcommand{\subsectionnameplural}{}
\newcommand{\subsectionnameshort}{}
\newcommand{\figurenameshort}{}
\newcommand{\tablenameshort}{}
\newcommand{\eqname}{}
\newcommand{\eqnameshort}{}
\newcommand{\defname}{}
\newcommand{\assumname}{}
\newcommand{\thmname}{}
\newcommand{\propname}{}
\newcommand{\lemmaname}{}
\newcommand{\remarkname}{}
\newcommand{\examplename}{}
\newcommand{\exercisename}{}

\addto\captionsgerman{%
	\renewcommand{\chapternameshort}{Kap.}
	\renewcommand{\sectionname}{Abschnitt}
	\renewcommand{\sectionnameshort}{Abschn.}
	\renewcommand{\subsectionname}{Unterabschnitt}
	\renewcommand{\subsectionnameplural}{Unterabschnitte}
	\renewcommand{\subsectionnameshort}{Unterabschn.}
	\renewcommand{\figurenameshort}{Abb.}
	\renewcommand{\tablenameshort}{Tab.}
	\renewcommand{\eqname}{Gleichung}
	\renewcommand{\eqnameshort}{Gl.}
	\renewcommand{\defname}{Definition}
	\renewcommand{\assumname}{Hypothese}
	\renewcommand{\thmname}{Satz}
	\renewcommand{\propname}{Proposition}
	\renewcommand{\lemmaname}{Lemma}
	\renewcommand{\remarkname}{Bemerkung}
	\renewcommand{\examplename}{Beispiel}
	\renewcommand{\exercisename}{Übung}
}
\addto\captionsdanish{%
	\renewcommand{\chapternameshort}{Kap.}
	\renewcommand{\sectionname}{Sektion}
	\renewcommand{\sectionnameshort}{Sek.}
	\renewcommand{\subsectionname}{Undersektion}
	\renewcommand{\subsectionnameplural}{Undersectioner}
	\renewcommand{\subsectionnameshort}{Undersek.}
	\renewcommand{\figurenameshort}{Fig.}
	\renewcommand{\tablenameshort}{Tab.}
	\renewcommand{\eqname}{Formel}
	\renewcommand{\eqnameshort}{Frml.}
	\renewcommand{\defname}{Definition}
	\renewcommand{\assumname}{Hypotese}
	\renewcommand{\thmname}{Sætning}
	\renewcommand{\propname}{Postulat}
	\renewcommand{\lemmaname}{Hjælpesætning}
	\renewcommand{\remarkname}{Bemærkning}
	\renewcommand{\examplename}{Eksempel}
	\renewcommand{\exercisename}{Øvelse}
}
\addto\captionsenglish{%
	\renewcommand{\chapternameshort}{Chap.}
	\renewcommand{\sectionname}{Section}
	\renewcommand{\sectionnameshort}{Sec.}
	\renewcommand{\subsectionname}{Subsection}
	\renewcommand{\subsectionnameplural}{Subsections}
	\renewcommand{\subsectionnameshort}{Subsec.}
	\renewcommand{\figurenameshort}{Fig.}
	\renewcommand{\tablenameshort}{Tab.}
	\renewcommand{\eqname}{Equation}
	\renewcommand{\eqnameshort}{Eq.}
	\renewcommand{\defname}{Definition}
	\renewcommand{\assumname}{Assumption}
	\renewcommand{\thmname}{Theorem}
	\renewcommand{\propname}{Proposition}
	\renewcommand{\lemmaname}{Lemma}
	\renewcommand{\remarkname}{Remark}
	\renewcommand{\examplename}{Example}
	\renewcommand{\exercisename}{Exercise}
}
\addto\captionsitalian{%
	\renewcommand{\chapternameshort}{Cap.}
	\renewcommand{\sectionname}{Sezione}
	\renewcommand{\sectionnameshort}{Sez.}
	\renewcommand{\subsectionname}{Sottosezione}
	\renewcommand{\subsectionnameplural}{Sottosezioni}
	\renewcommand{\subsectionnameshort}{Sottosezione}
	\renewcommand{\figurenameshort}{Fig.}
	\renewcommand{\tablenameshort}{Tab.}
	\renewcommand{\eqname}{Equazione}
	\renewcommand{\eqnameshort}{Eq.}
	\renewcommand{\defname}{Definizione}
	\renewcommand{\assumname}{Assunzione}
	\renewcommand{\thmname}{Teorema}
	\renewcommand{\propname}{Proposizione}
	\renewcommand{\lemmaname}{Lemma}
	\renewcommand{\remarkname}{Osservazione}
	\renewcommand{\examplename}{Esempio}
	\renewcommand{\exercisename}{Esercizio}
}
\addto\captionsrussian{%
	\renewcommand{\chapternameshort}{Гл.}
	\renewcommand{\sectionname}{Раздел}
	\renewcommand{\sectionnameshort}{Разд.}
	\renewcommand{\subsectionname}{Подраздел}
	\renewcommand{\subsectionnameplural}{Подразделы}
	\renewcommand{\subsectionnameshort}{Подразд.}
	\renewcommand{\figurenameshort}{Рис.}
	\renewcommand{\tablenameshort}{Табл.}
	\renewcommand{\eqname}{Формула}
	\renewcommand{\eqnameshort}{Ф-ла}
	\renewcommand{\defname}{Определение}
	\renewcommand{\assumname}{Гипотеза}
	\renewcommand{\thmname}{Теорема}
	\renewcommand{\propname}{Утверждение}
	\renewcommand{\lemmaname}{Лемма}
	\renewcommand{\remarkname}{Замечание}
	\renewcommand{\examplename}{Пример}
	\renewcommand{\exercisename}{Упражнение}
}
% Do you speak other languages? Please, feel free to add the captions!


%----------------------------------------------------------------------------------------
%	LABELLING COMMANDS
%----------------------------------------------------------------------------------------

\newcommand{\labpage}[1]{\label{page:#1}}
\newcommand{\labpart}[1]{\label{part:#1}}
\newcommand{\labch}[1]{\label{ch:#1}}
\newcommand{\labsec}[1]{\label{sec:#1}}
\newcommand{\labsubsec}[1]{\label{subsec:#1}}
\newcommand{\labfig}[1]{\label{fig:#1}}
\newcommand{\labtab}[1]{\label{tab:#1}}
\newcommand{\labeq}[1]{\label{eq:#1}}
\newcommand{\labdef}[1]{\label{def:#1}}
\newcommand{\labthm}[1]{\label{thm:#1}}
\newcommand{\labassum}[1]{\label{assum:#1}}
\newcommand{\labprop}[1]{\label{prop:#1}}
\newcommand{\lablemma}[1]{\label{lemma:#1}}
\newcommand{\labremark}[1]{\label{remark:#1}}
\newcommand{\labexample}[1]{\label{example:#1}}
\newcommand{\labexercise}[1]{\label{exercise:#1}}


%----------------------------------------------------------------------------------------
%	REFERENCING COMMANDS
%----------------------------------------------------------------------------------------

\newcommand{\refpage}[1]{\hyperref[#1]{\pagename}\xspace\pageref{page:#1}} % Page 84
\newcommand{\vrefpage}[1]{\vpageref*{page:#1}} % on the following page, on page 84

% For unnumbered parts
\newcommand{\arefpart}[1]{\hyperref[part:#1]{\partname}\xspace`\nameref{part:#1}'} % Part `Name of the Part'
\newcommand{\avrefpart}[1]{\hyperref[part:#1]{\partname}\xspace`\nameref{part:#1}' \vpageref{part:#1}} % Part `Name of the Part' on page 84

% For numbered parts
\newcommand{\refpart}[1]{\hyperref[part:#1]{\partname}\xspace\ref{part:#1}} % Part IV
\newcommand{\vrefpart}[1]{\hyperref[part:#1]{\partname}\xspace\vref{part:#1}} % Part IV, Part IV on the following page, Part IV on page 84
\newcommand{\nrefpart}[1]{\hyperref[part:#1]{\partname}\xspace\ref{part:#1} (\nameref{part:#1})}
\newcommand{\frefpart}[1]{\hyperref[part:#1]{\partname\xspace\ref{part:#1} (\nameref{part:#1}) \vpageref{part:#1}}} % Part IV (Name of the Part), Part IV (Name of the Part) on the following page, Part IV (Name of the Part) on page 84)

%\newcommand{\refch}[1]{\hyperref[#1]{\chaptername\xspace\usekomafont{chapter}\normalsize\nameref{ch:#1}}\xspace\vpageref{ch:#1}\,}
\newcommand{\refchshort}[1]{\hyperref[ch:#1]{\chapternameshort\xspace\ref{ch:#1}}}
\newcommand{\refch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1}}}
\newcommand{\vrefch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1} \vpageref{ch:#1}}}
\newcommand{\nrefch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1} (\nameref{ch:#1})}}
\newcommand{\frefch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1} (\nameref{ch:#1}) \vpageref{ch:#1}}}

%\newcommand{\refsec}[1]{Section~{\usekomafont{section}\normalsize\nameref{sec:#1}}\xspace\vpageref{sec:#1}\,}
\newcommand{\refsecshort}[1]{\hyperref[sec:#1]{\sectionnameshort\xspace\ref{sec:#1}}}
\newcommand{\refsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\ref{sec:#1}}}
\newcommand{\vrefsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\vref{sec:#1}}}
\newcommand{\nrefsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\ref{sec:#1} (\nameref{sec:#1})}}
\newcommand{\frefsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\ref{sec:#1} (\nameref{sec:#1}) \vpageref{sec:#1}}}

\newcommand{\refsubsecshort}[1]{\hyperref[subsec:#1]{\sectionnameshort\xspace\ref{subsec:#1}}}
\newcommand{\refsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\ref{subsec:#1}}}
\newcommand{\vrefsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\vref{subsec:#1}}}
\newcommand{\nrefsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\ref{subsec:#1} (\nameref{subsec:#1})}}
\newcommand{\frefsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\ref{subsec:#1} (\nameref{subsec:#1}) \vpageref{subsec:#1}}}

%\newcommand{\reffig}[1]{{\hypersetup{colorlinks=false}\usekomafont{captionlabel}\hyperref[fig:#1]{Figure}\xspace\ref{fig:#1}}}
\newcommand{\reffigshort}[1]{\hyperref[fig:#1]{\figurenameshort\xspace\ref{fig:#1}}}
\newcommand{\reffig}[1]{\hyperref[fig:#1]{\figurename}\xspace\ref{fig:#1}}
\newcommand{\vreffig}[1]{\hyperref[fig:#1]{\figurename\xspace\vref{fig:#1}}}

%\newcommand{\reftab}[1]{{\hypersetup{colorlinks=false}\usekomafont{captionlabel}\hyperref[tab:#1]{Table}\xspace\ref{tab:#1}}}
\newcommand{\reftab}[1]{\hyperref[tab:#1]{\tablename}\xspace\ref{tab:#1}}
\newcommand{\vreftab}[1]{\hyperref[tab:#1]{\tablename\xspace\vref{tab:#1}}}

\newcommand{\refeqshort}[1]{\hyperref[eq:#1]\eqnameshort\xspace(\ref{eq:#1})}
\newcommand{\refeq}[1]{\hyperref[eq:#1]\eqname\xspace\ref{eq:#1}}
\newcommand{\vrefeq}[1]{\hyperref[eq:#1]\eqname\xspace\vref{eq:#1}}

\newcommand{\refdef}[1]{\hyperref[def:#1]\defname\xspace\ref{def:#1}}
\newcommand{\vrefdef}[1]{\hyperref[def:#1]\defname\xspace\vref{def:#1}}

\newcommand{\refassum}[1]{\hyperref[assum:#1]\assumname\xspace\ref{assum:#1}}
\newcommand{\vrefassum}[1]{\hyperref[assum:#1]\assumname\xspace\vref{assum:#1}}

\newcommand{\refthm}[1]{\hyperref[thm:#1]\thmname\xspace\ref{thm:#1}}
\newcommand{\vrefthm}[1]{\hyperref[thm:#1]\thmname\xspace\vref{thm:#1}}

\newcommand{\refprop}[1]{\hyperref[prop:#1]\propname\xspace\ref{prop:#1}}
\newcommand{\vrefprop}[1]{\hyperref[prop:#1]\propname\xspace\vref{prop:#1}}

\newcommand{\reflemma}[1]{\hyperref[lemma:#1]\lemmaname\xspace\ref{lemma:#1}}
\newcommand{\vreflemma}[1]{\hyperref[lemma:#1]\lemmaname\xspace\vref{lemma:#1}}

\newcommand{\refremark}[1]{\hyperref[remark:#1]\remarkname\xspace\ref{remark:#1}}
\newcommand{\vrefremark}[1]{\hyperref[remark:#1]\remarkname\xspace\vref{remark:#1}}

\newcommand{\refexample}[1]{\hyperref[example:#1]\examplename\xspace\ref{example:#1}}
\newcommand{\vrefexample}[1]{\hyperref[example:#1]\examplename\xspace\vref{example:#1}}

\newcommand{\refexercise}[1]{\hyperref[exercise:#1]\exercisename\xspace\ref{exercise:#1}}
\newcommand{\vrefexercise}[1]{\hyperref[exercise:#1]\exercisename\xspace\vref{exercise:#1}}


%----------------------------------------------------------------------------------------
%	CLEVEREF CUSTOMISATION
%----------------------------------------------------------------------------------------

% Hyperlink the page reference as well
\let\oldvpageref\vpageref
\renewcommand{\vpageref}[1]{\hyperref[#1]{\oldvpageref{#1}}}

% Remove parentheses around equations
\creflabelformat{equation}{#2\textup{#1}#3}

% Set the refname for subsections
\crefname{subsection}{\subsectionname}{\subsectionnameplural}
\Crefname{subsection}{\subsectionname}{\subsectionnameplural}
